package com.xuchaotong.utils;

import com.alibaba.fastjson.JSON;
import com.xuchaotong.entity.EvaluationsComparsionStatistics;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.xuchaotong.entity.ItemLabelDTO;
import com.xuchaotong.utils.bean.Princess;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
 * @author EASON.TONG
 */
@Slf4j
public class ArrayTest {

    private List<EvaluationsComparsionStatistics> list;

    @Before
    public void init() {
        list = EvaluationsComparsionStatistics.build();
    }

    @Test
    public void testGroup() {
        Map<Long, List<EvaluationsComparsionStatistics>> group = list.stream()
                .collect(Collectors.groupingBy(EvaluationsComparsionStatistics::getScoreId));

        Map<Long, Long> collect1 = list.parallelStream().collect(Collectors.groupingBy(EvaluationsComparsionStatistics::getScoreId, Collectors.counting()));

        System.out.println(JSON.toJSON(collect1));
    }

    @Test
    public void testToMap() {
        /*Map<Integer, Integer> map = list.stream()
            .collect(Collectors.toMap(EvaluationsComparsionStatistics::getScoreValue,
                EvaluationsComparsionStatistics::getQuantity));*/

        Map<Integer, EvaluationsComparsionStatistics> map = list.stream()
                .collect(Collectors.toMap(EvaluationsComparsionStatistics::getScoreValue,
                        entity -> entity));

        System.out.println(JSON.toJSON(map));
    }

    @Test
    public void testProperty() {
        List<Long> ids = list.stream().map(item -> item.getEvaluationTypeId()).distinct()
                .collect(Collectors.toList()).stream().filter(item -> item != null).collect(Collectors.toList());
        System.out.println(JSON.toJSON(ids));
    }

    @Test
    public void testCalculation() {
        Integer totoal = list.stream().mapToInt(EvaluationsComparsionStatistics::getQuantity).sum();
        System.out.println(totoal);

        Integer total1 = list.stream()
                .reduce(0, (x, y) -> x + (y.getScoreValue() * y.getQuantity()), Integer::sum);
        System.out.println(total1);
    }

    @Test
    public void testStatistics() {
        Map<String, List<EvaluationsComparsionStatistics>> group = list.stream()
                .collect(Collectors.groupingBy(EvaluationsComparsionStatistics::getScoreName));

        Map<Integer, Double> scoreMap = new HashMap<Integer, Double>(5) {{
            put(1, 0.00);
            put(2, 0.00);
            put(3, 0.00);
            put(4, 0.00);
            put(5, 0.00);
        }};

        Map<String, Map<Integer, Double>> perMap = new HashMap<>();

        group.forEach((key, value) -> {
            Integer total = value.stream().mapToInt(EvaluationsComparsionStatistics::getQuantity)
                    .sum();
            Map<Integer, Integer> map = value.stream()
                    .collect(Collectors.toMap(EvaluationsComparsionStatistics::getScoreValue,
                            EvaluationsComparsionStatistics::getQuantity));

            map.forEach((score, count) -> {
                if (scoreMap.containsKey(score)) {
                    DecimalFormat df = new DecimalFormat("0.00");
                    Double result = Double.valueOf(df.format((float) count / total));
                    scoreMap.put(score, result);
                }
            });

            perMap.put(key, scoreMap);

        });

        System.out.println(JSON.toJSON(perMap));

    }

    @Test
    public void testFilter() {
        List<EvaluationsComparsionStatistics> lst = list.stream().filter(item -> item.getScoreId().equals(12L)).collect(Collectors.toList());
        System.out.println(lst.size());
        System.out.println(JSON.toJSON(lst));
    }

    @Test
    public void testSort() {
        List<ItemLabelDTO> list = ItemLabelDTO.buildList();

        ItemLabelDTO item = list.stream().max(Comparator.comparing(ItemLabelDTO::getCreateTime)).get();
        ItemLabelDTO item1 = list.stream().max(Comparator.comparing(ItemLabelDTO::getLabel)).get();

//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//        log.info(item.getCreateTime().format(formatter));

        log.info(item1.getLabel());
    }
}
